home *** CD-ROM | disk | FTP | other *** search
/ Mac Cube 4: Multimedia Applications / MacCube Volume 4: Multimedia Applications.iso / Graphics / POV Ray / POVSOURCE / SOURCE / ProgressDialog.c < prev    next >
Text File  |  1994-02-04  |  7KB  |  225 lines

  1. /*
  2. ==============================================================================
  3. Project:    POV-Ray
  4.  
  5. Version:    2.2
  6.  
  7. File Name:    ProgressDialog.c
  8.  
  9. Description:
  10.     General-purpose progress bar handling for dialogs.
  11.  
  12.     This is the main source file, containing the private definitions and
  13.     code to implement all the needed external and internal support functions.
  14.  
  15. Related Files:
  16.     ProgressDialog.h    - Header for these routines
  17. ------------------------------------------------------------------------------
  18. Author:
  19.     Eduard [esp] Schwan
  20. ------------------------------------------------------------------------------
  21.     from Persistence of Vision Raytracer
  22.     Copyright 1993 Persistence of Vision Team
  23. ------------------------------------------------------------------------------
  24.     NOTICE: This source code file is provided so that users may experiment
  25.     with enhancements to POV-Ray and to port the software to platforms other 
  26.     than those supported by the POV-Ray Team.  There are strict rules under
  27.     which you are permitted to use this file.  The rules are in the file
  28.     named POVLEGAL.DOC which should be distributed with this file. If 
  29.     POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  30.     Team Coordinator by leaving a message in CompuServe's Graphics Developer's
  31.     Forum.  The latest version of POV-Ray may be found there as well.
  32.  
  33.     This program is based on the popular DKB raytracer version 2.12.
  34.     DKBTrace was originally written by David K. Buck.
  35.     DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  36. ------------------------------------------------------------------------------
  37. More Info:
  38.     This Macintosh version of POV-Ray was created and compiled by Jim Nitchals
  39.     (Think 5.0) and Eduard Schwan (MPW 3.2), based (loosely) on the original
  40.     port by Thomas Okken and David Lichtman, with some help from Glenn Sugden.
  41.  
  42.     For bug reports regarding the Macintosh version, you should contact:
  43.     Eduard [esp] Schwan
  44.         CompuServe: 71513,2161
  45.         Internet: jl.tech@applelink.apple.com
  46.         AppleLink: jl.tech
  47.     Jim Nitchals
  48.         Compuserve: 73117,3020
  49.         America Online: JIMN8
  50.         Internet: jimn8@aol.com -or- jimn8@applelink.apple.com
  51.         AppleLink: JIMN8
  52. ------------------------------------------------------------------------------
  53. Change History:
  54.     920820    [esp]    Created
  55.     920920    [esp]    changed highestVal from int to long
  56.     930619    [esp]    added dispose fn
  57.     930903    [esp]    Worked on display bug in showProgress_UProc
  58.     930911    [esp]    Finally fixed the display bug in showProgress_UProc
  59.     931001    [esp]    version 2.0 finished (Released on 10/4/93)
  60.     931119    [djh]    2.0.1 conditionally compiles for PPC machine, keyword __powerc
  61. ==============================================================================
  62. */
  63.  
  64. /* Macintosh-specific headers */
  65. #include <Types.h>
  66. #include <Dialogs.h>
  67. #include <QuickDraw.h>
  68.  
  69. #include "POVMac.h"            // RoutineDescriptors & MixedMode.h
  70. #include "ProgressDialog.h"    // our defs
  71.  
  72.  
  73. /*  gProgressItemValue will be set to 0 to 100 by CalculateProgressValue() */
  74.  
  75. static unsigned long    gProgressItemValue;
  76. static short            gProgressItemNum;
  77. static Rect                gProgressItemRect;
  78. static RgnHandle        gProgressItemRgn = NULL;
  79.  
  80.  
  81. // ==============================================
  82. // Set up a user item proc for drawing progress bar
  83. pascal void showProgress_UProc(DialogPtr theDialog, short theItem)
  84. {
  85. #pragma unused (theItem)
  86.     PenState    SavePen;
  87.     short        progressPos;
  88.     Rect        dispRect,
  89.                 outerRect,
  90.                 progressRect;
  91.  
  92.     // remember original penstate
  93.     SetPort(theDialog);
  94.     GetPenState(&SavePen);
  95.  
  96.     // find progress bar rectangle
  97.     dispRect = gProgressItemRect;
  98.  
  99.     // outer frame
  100.     outerRect = dispRect;
  101.     InsetRect(&outerRect, 1, 1);
  102.     PenSize(1, 1);
  103.     ForeColor(blackColor);
  104.     FrameRect(&outerRect);
  105.  
  106.     // set up progress rect
  107.     progressRect = dispRect;
  108.     InsetRect(&progressRect, 3, 3);
  109.  
  110.     // calculate inner bar progress position
  111.     if (gProgressItemValue > 100)
  112.         gProgressItemValue = 100;
  113.     progressPos = ((unsigned long)(progressRect.right - progressRect.left) * gProgressItemValue) / 100L;
  114.  
  115.     // draw inner bar (left filled side)
  116.     if (progressPos > 0)
  117.     {
  118.         progressRect.right = dispRect.left + progressPos;
  119.         ForeColor(blackColor);
  120.         PaintRect(&progressRect);
  121.  
  122.         // draw inner bar (right open side)
  123.         if (progressPos < 100)
  124.         {
  125.             progressRect.left = dispRect.left + progressPos;
  126.             ForeColor(whiteColor);
  127.             PaintRect(&progressRect);
  128.         }
  129.     }
  130.  
  131.     // restore state
  132.     SetPenState(&SavePen);
  133.  
  134. } // showProgress_UProc
  135.  
  136.  
  137. // ==============================================
  138. // Sets dialog item's display proc to draw progress bar
  139. static void SetupProgressItem(DialogPtr theDialog, short theItemNum)
  140. {
  141.     short    itemtype;
  142.     Handle    tempHandle;
  143.  
  144.     gProgressItemValue = 0;
  145.  
  146.     // Set up User item to display a progress bar
  147.     GetDItem(theDialog, theItemNum, &itemtype, &tempHandle, &gProgressItemRect);
  148. #if defined(__powerc)
  149.     SetDItem(theDialog, theItemNum, itemtype, (Handle)&gShowProgressRD, &gProgressItemRect);
  150. #else
  151.     SetDItem(theDialog, theItemNum, itemtype, (Handle)&showProgress_UProc, &gProgressItemRect);
  152. #endif
  153.  
  154.     // remember.. for later updates
  155.     gProgressItemRgn = NewRgn();
  156.     RectRgn(gProgressItemRgn, &gProgressItemRect);
  157.     gProgressItemNum = theItemNum;
  158. } // SetupProgressItem
  159.  
  160.  
  161. // ==============================================
  162. // Loads Dialog resource and sets up progress bar user item proc
  163. DialogPtr GetNewProgressDialog(short theDialogID, short theProgressItemNum)
  164. {
  165.     DialogPtr    theDialog = NULL;
  166.  
  167.     theDialog = GetNewDialog(theDialogID, NULL, (WindowPtr)-1);
  168.  
  169.     if (theDialog)
  170.         SetupProgressItem(theDialog, theProgressItemNum);
  171.  
  172.     return (theDialog);
  173.  
  174. } // GetNewProgressDialog
  175.  
  176.  
  177. // ==============================================
  178. // Calculates current value for progress bar
  179. static void CalculateProgressValue(long lowestVal, long highestVal, long currentVal)
  180. {
  181.     // Clip against upper & lower bounds
  182.     if (currentVal < lowestVal)
  183.         currentVal = lowestVal;
  184.     else
  185.         if (currentVal > highestVal)
  186.             currentVal = highestVal;
  187.  
  188.     // Now calculate current value
  189.     gProgressItemValue = (currentVal-lowestVal) * 100L / (highestVal-lowestVal);
  190.  
  191. } // CalculateProgressValue
  192.  
  193.  
  194. // ==============================================
  195. // Recalculate progress bar and redisplay dialog
  196. void updateProgressDialog(DialogPtr pDialogPtr, long lowestVal, long highestVal, long currentVal)
  197. {
  198.     CalculateProgressValue(lowestVal, highestVal, currentVal);
  199.     SetPort(pDialogPtr);
  200.     InvalRect(&gProgressItemRect);
  201.  
  202.     BeginUpdate(pDialogPtr);
  203.     UpdateDialog(pDialogPtr, gProgressItemRgn);
  204.     EndUpdate(pDialogPtr);
  205.  
  206. } // updateProgressDialog
  207.  
  208.  
  209. // ==============================================
  210. // dispose the progress bar dialog
  211. void disposeProgressDialog(DialogPtr pDialogPtr)
  212. {
  213.     if (pDialogPtr)
  214.     {
  215.         DisposeDialog(pDialogPtr);
  216.         pDialogPtr = NULL;
  217.     }
  218.  
  219.     if (gProgressItemRgn)
  220.     {
  221.         DisposeRgn(gProgressItemRgn);
  222.         gProgressItemRgn = NULL;
  223.     }
  224. } // disposeProgressDialog
  225.